# Makefile
# @author Theobaldo

default: all

# Developing a project with this makefile:
# - Use .cpp as the extension for your source files.
# - Place all .cpp files with a main function directly in the src directory.
# - Place all .cpp files without a main function in subdirectories of the src directory.
# - For including your own header files, specify the path relative to the src directory.
#	E.g. #include "util/GlobalLogger.h"
# - Check the compiler variables INCLUDE, LIBPATH and LIBS.
# - Make sure UNIX find is installed and on your path.

#######################################################################################
# Variables
#######################################################################################

# compiler variables
INCLUDE = -Iexternal/include -Isrc
LIBPATH = -Lexternal/lib
LIBS = -lPocoFoundation -lPocoXML -lPocoUtil -lPocoNet
UNAME := $(shell uname)

# archive name
TARBALL = IGS

# subdirectories of the 'src' directory
SUBDIRS = $(shell find src -mindepth 1 -type d ! -path '*.svn*')

# source files with a main function are in the src directory
PROGSOURCES = $(shell find src -maxdepth 1 -type f -path '*.cpp')

# source files without a main function are in subdirectories of the src subdirectory
OBJSOURCES = $(shell find src -mindepth 2 -type f -path '*.cpp')

# object files for sources without a main function
ALLOBJECTS = $(OBJSOURCES:src/%.cpp=bin/%.o)

# executables for sources with a main function
ALLPROGRAMS = $(PROGSOURCES:src/%.cpp=bin/%.exe)

# Prevent deleting of intermediate .o files
.SECONDARY: $(ALLOBJECTS) $(ALLPROGRAMS:%.exe=%.o)

#######################################################################################
# Global Non-File Targets
#######################################################################################

# non-file targets
.PHONY: default all compile clean tar outputdirs options doc

options:
	@echo 
	@echo Inleiding gedistribueerde systemen
	@echo 
	@echo "	Choose one of the following:"
	@echo 
	@echo "	->  make compile : Compile the project"
	@echo "	->  make all : Compile and link the project"
	@echo "	->  make clean : Clean the directory"
	@echo "	->  make tar : Make a tarball"
	@echo 

compile: $(ALLOBJECTS) $(ALLPROGRAMS:%.exe=%.o)

all: $(ALLPROGRAMS)
	@echo Compilation complete

# Clean
clean:
	@rm -rf bin doc *~ *.bak external/*~ external/*.bak src/*~ src/*.bak src/*/*~ src/*/*.bak *.log */*.log *.dat
	@echo Directory cleaned.

# Making a tarball
tar: clean
	@rm -f $(TARBALL).tgz
	@find -type f | xargs chmod 644
	@tar -czf $(TARBALL).tgz ../IGS --preserve --exclude=.svn --exclude=$(TARBALL).tgz 2>/dev/null
	@chmod 644 $(TARBALL).tgz
	@echo "$(TARBALL).tgz made!"

#######################################################################################
# File Targets
#######################################################################################

# output directories (specified by a .touch file in each directory)
outputdirs: | bin/.touch $(SUBDIRS:src/%=bin/%/.touch)
%/.touch:
	mkdir $(dir $@)
	@touch $@

# Include header dependencies (if they exist)
-include $(ALLOBJECTS:%.o=%.d)
-include $(ALLPROGRAMS:%.exe=%.d)

# object files
bin/%.o: src/%.cpp external/include/Poco | outputdirs
	@echo 'Building $(<:src/%=%)'
	@g++ -O3 -Wall -c $(INCLUDE) -MMD -ggdb -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"

# executables
bin/%.exe: external/lib bin/%.o $(ALLOBJECTS)
	@echo 'Linking $@'
	@g++ -ggdb -o "$@" "$(@:%.exe=%.o)" $(ALLOBJECTS) $(LIBPATH) $(LIBS)
